// pages/myself/merchant/NewStore.js
let constants = null;
let network = null;
let utils = null;
let api = null;
var Zan = require('../../../dist/index');
let WxNotificationCenter = require('../../../utils/WxNotificationCenter.js');
// 获取应用实例
let app = getApp();

Page(Object.assign({}, Zan.Toast, Zan.TopTips, {

  /**
   * 页面的初始数据
   */
  data: {
    storeName: null,
    province: '',
    city: '',
    district: '',
    address: '',
    area: null,
    phone: null,
    clientLeft: 10,
    clientRight: 10,
    winWidth: 0,
    weekL: '周一',
    weekR: '周日',
    timeLeft: 10,
    timeRight: 10,
    timeL: '00:00',
    timeR: '23:59',
    businessHours: null,
    isshowArea: true,
    shangquan: [],
    circle: null,
    circles: [], // 滑轮是使用
    isSubmit: false,
    store: null,
    businessAreaId: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    constants = new app.Constants();
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();

    this.setData({
      winWidth: utils.windowSize().winWidth,
      brandId: options.brandId
    })
    if (options.store) {
      wx.setNavigationBarTitle({
        title: '编辑门店',
      })
      let store = JSON.parse(options.store);
      this.setData({
        store: store,
        circle: store.businessArea.globalName,
        province: store.businessArea.province,
        city: store.businessArea.city,
        district: store.businessArea.district,
        area: store.businessArea.province + store.businessArea.city + store.businessArea.district,
        address: store.address.replace(store.businessArea.province, '').replace(store.businessArea.city, '').replace(store.businessArea.district, ''),
        lng: store.lng,
        lat: store.lat,
        weekL: store.openDay,
        weekR: store.closeDay,
        timeL: store.openTime,
        timeR: store.closeTime,
        businessHours: store.openDay + '至' + store.closeDay + ' ' + store.openTime + '-' + store.closeTime,
        businessAreaId: store.businessAreaId,
        id: store.id,
        isshowArea: false
      });
      network._post_json(this, api.getBusinessAreaList, { city: store.businessArea.city, district: store.businessArea.district }, Success);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },

  /**
   * 获取位置
   */
  location: function () {
    let that = this;
    wx.chooseLocation({
      success: function (res) {
        if (res.address) {
          let latitude = res.latitude;
          let longitude = res.longitude;
          let address = res.address;
          that.setData({
            lng: longitude,
            lat: latitude
          })
          // 获取规范的地理信息
          wx.request({
            url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${constants.key}`,
            success: res => {
              console.log(res)
              let info = res.data.result.ad_info;
              let province = info.province;
              let city = info.city;
              let district = info.district;
              that.setData({
                province: province,
                city: city,
                district: district,
                area: province + city + district,
                address: address.replace(province, '').replace(city, '').replace(district, ''),
                circles: []
              })
              network._post_json(that, api.getBusinessAreaList, { city: that.data.city, district: that.data.district }, Success);

            }
          })
        } else {
          that.showZanToast('请选择地点！')
        }

      }
    })


  },

  //选择商圈
  bindAreaChange: function (e) {
    this.setData({
      businessAreaId: this.data.shangquan[e.detail.value].id,
      circle: this.data.shangquan[e.detail.value].globalName
    })
  },

  /**
   * 弹出框开关
   */
  toggleDialog() {
    this.setData({
      showDialog: !this.data.showDialog
    })
  },

  bindKeyInput: function (e) {
    this.setData({
      address: e.detail.value
    })
  },

  /**
   * 确定
   */
  confirmDialog() {
    this.setData({
      showDialog: !this.data.showDialog,
      businessHours: this.data.weekL + '至' + this.data.weekR + ' ' + this.data.timeL + '-' + this.data.timeR
    })
  },

  //新增门店提交
  formSubmit: function (e) {
    this.setData({
      name: e.detail.value.name,
      phone: e.detail.value.phone
    })
    isSubmit(this)
    if (this.data.isSubmit) {
      var postdata = {
        id: this.data.id,
        brandId: this.data.brandId,
        name: e.detail.value.name,
        phone: e.detail.value.phone,
        province: this.data.province,
        city: this.data.city,
        district: this.data.district,
        address: this.data.province + this.data.city + this.data.district + this.data.address,
        businessAreaId: this.data.businessAreaId,
        openDay: this.data.weekL,
        closeDay: this.data.weekR,
        openTime: this.data.timeL,
        closeTime: this.data.timeR,
        lng: this.data.lng,
        lat: this.data.lat,
        appId: app.globalData.appId
      }
      if (this.data.store) {
        network._post_json(this, api.modifyStore, postdata, formSuccess);
      } else {
        network._post_json(this, api.addStore, postdata, formSuccess);
      }
    } else {
      this.showZanToast('请填写所有信息！')
    }

  },

  /**
   * 周左滑块
   */
  moveWeekLeft: function (e) {
    let temp = e.changedTouches[0].clientX;
    if (temp >= this.data.winWidth * 0.1) {
      if (temp / this.data.winWidth * 100 < (100 - this.data.clientRight - 10)) {
        let x = temp / this.data.winWidth * 100
        this.setData({
          clientLeft: x
        })
        if (x <= 16.67) {
          this.setData({
            weekL: '周一'
          })
        } else if (x <= 30) {
          this.setData({
            weekL: '周二'
          })
        } else if (x <= 43.33) {
          this.setData({
            weekL: '周三'
          })
        } else if (x <= 56.66) {
          this.setData({
            weekL: '周四'
          })
        } else if (x <= 69.99) {
          this.setData({
            weekL: '周五'
          })
        } else if (x <= 82.32) {
          this.setData({
            weekL: '周六'
          })
        }

      }
    }
  },

  /**
   * 周右滑块
   */
  moveWeekRight: function (e) {
    let temp = e.changedTouches[0].clientX;
    if (temp <= this.data.winWidth * 0.9) {
      if (temp / this.data.winWidth * 100 > this.data.clientLeft + 10) {
        let x = (this.data.winWidth - temp) / this.data.winWidth * 100
        this.setData({
          clientRight: x
        })
        if (x <= 16.67) {
          this.setData({
            weekR: '周日'
          })
        } else if (x <= 30) {
          this.setData({
            weekR: '周六'
          })
        } else if (x <= 43.33) {
          this.setData({
            weekR: '周五'
          })
        } else if (x <= 56.66) {
          this.setData({
            weekR: '周四'
          })
        } else if (x <= 69.99) {
          this.setData({
            weekR: '周三'
          })
        } else if (x <= 82.32) {
          this.setData({
            weekR: '周二'
          })
        }
      }
    }
  },

  /**
   * 时间左滑块
   */
  moveTimeLeft: function (e) {
    let temp = e.changedTouches[0].clientX;
    if (temp >= this.data.winWidth * 0.1) {
      if (temp / this.data.winWidth * 100 < (100 - this.data.timeRight - 10)) {
        let x = temp / this.data.winWidth * 100
        this.setData({
          timeLeft: x
        })
        if (x <= 11.67) {
          this.setData({
            timeL: '00:00'
          })
        } else if (x <= 15) {
          this.setData({
            timeL: '01:00'
          })
        } else if (x <= 18.33) {
          this.setData({
            timeL: '02:00'
          })
        } else if (x <= 21.66) {
          this.setData({
            timeL: '03:00'
          })
        } else if (x <= 24.99) {
          this.setData({
            timeL: '04:00'
          })
        } else if (x <= 28.32) {
          this.setData({
            timeL: '05:00'
          })
        } else if (x <= 31.65) {
          this.setData({
            timeL: '06:00'
          })
        } else if (x <= 34.98) {
          this.setData({
            timeL: '07:00'
          })
        } else if (x <= 38.31) {
          this.setData({
            timeL: '08:00'
          })
        } else if (x <= 41.64) {
          this.setData({
            timeL: '09:00'
          })
        } else if (x <= 44.97) {
          this.setData({
            timeL: '10:00'
          })
        } else if (x <= 48.3) {
          this.setData({
            timeL: '11:00'
          })
        } else if (x <= 51.63) {
          this.setData({
            timeL: '12:00'
          })
        } else if (x <= 54.96) {
          this.setData({
            timeL: '13:00'
          })
        } else if (x <= 58.29) {
          this.setData({
            timeL: '14:00'
          })
        } else if (x <= 61.62) {
          this.setData({
            timeL: '15:00'
          })
        } else if (x <= 64.95) {
          this.setData({
            timeL: '16:00'
          })
        } else if (x <= 68.28) {
          this.setData({
            timeL: '17:00'
          })
        } else if (x <= 71.61) {
          this.setData({
            timeL: '18:00'
          })
        } else if (x <= 74.94) {
          this.setData({
            timeL: '19:00'
          })
        } else if (x <= 78.27) {
          this.setData({
            timeL: '20:00'
          })
        } else if (x <= 81.6) {
          this.setData({
            timeL: '21:00'
          })
        } else if (x <= 84.93) {
          this.setData({
            timeL: '22:00'
          })
        } else if (x <= 88.26) {
          this.setData({
            timeL: '23:00'
          })
        }
      }
    }
  },

  /**
   * 周右滑块
   */
  moveTimeRight: function (e) {
    let temp = e.changedTouches[0].clientX;
    if (temp <= this.data.winWidth * 0.9) {
      if (temp / this.data.winWidth * 100 > this.data.timeLeft + 10) {
        let x = (this.data.winWidth - temp) / this.data.winWidth * 100
        this.setData({
          timeRight: x
        })
        if (x <= 11.67) {
          this.setData({
            timeR: '23:59'
          })
        } else if (x <= 15) {
          this.setData({
            timeR: '23:00'
          })
        } else if (x <= 18.33) {
          this.setData({
            timeR: '22:00'
          })
        } else if (x <= 21.66) {
          this.setData({
            timeR: '21:00'
          })
        } else if (x <= 24.99) {
          this.setData({
            timeR: '20:00'
          })
        } else if (x <= 28.32) {
          this.setData({
            timeR: '19:00'
          })
        } else if (x <= 31.65) {
          this.setData({
            timeR: '18:00'
          })
        } else if (x <= 34.98) {
          this.setData({
            timeR: '17:00'
          })
        } else if (x <= 38.31) {
          this.setData({
            timeR: '16:00'
          })
        } else if (x <= 41.64) {
          this.setData({
            timeR: '15:00'
          })
        } else if (x <= 44.97) {
          this.setData({
            timeR: '14:00'
          })
        } else if (x <= 48.3) {
          this.setData({
            timeR: '13:00'
          })
        } else if (x <= 51.63) {
          this.setData({
            timeR: '12:00'
          })
        } else if (x <= 54.96) {
          this.setData({
            timeR: '11:00'
          })
        } else if (x <= 58.29) {
          this.setData({
            timeR: '10:00'
          })
        } else if (x <= 61.62) {
          this.setData({
            timeR: '09:00'
          })
        } else if (x <= 64.95) {
          this.setData({
            timeR: '08:00'
          })
        } else if (x <= 68.28) {
          this.setData({
            timeR: '07:00'
          })
        } else if (x <= 71.61) {
          this.setData({
            timeR: '06:00'
          })
        } else if (x <= 74.94) {
          this.setData({
            timeR: '05:00'
          })
        } else if (x <= 78.27) {
          this.setData({
            timeR: '04:00'
          })
        } else if (x <= 81.6) {
          this.setData({
            timeR: '03:00'
          })
        } else if (x <= 84.93) {
          this.setData({
            timeR: '02:00'
          })
        } else if (x <= 88.26) {
          this.setData({
            timeR: '01:00'
          })
        }
      }
    }
  }

}))

var Success = function (that, res) {
  if (res.data.data) {
    let businessAreas = res.data.data[0].businessAreas;
    let temp = [];
    if (businessAreas.length > 0) {
      for (let i = 0; i < businessAreas.length; i++) {
        temp.push(businessAreas[i].globalName);
      }
      that.setData({
        isshowArea: false,
        shangquan: businessAreas,
        circles: that.data.circles.concat(temp)
      })
    } else {
      that.showZanTopTips(that.prompt, '本区暂无商圈');
    }
  }
}

var formSuccess = function (that, res) {
  if (that.data.store) {
    that.showZanToast('门店修改成功！')
  } else {
    that.showZanToast('新增门店成功！')
    WxNotificationCenter.postNotificationName("RefreshStoreCount", 1);
    WxNotificationCenter.postNotificationName("RefreshMerchantBrand");
  }
  WxNotificationCenter.postNotificationName("RefreshStoreSet");
  setTimeout(function () {
    wx.navigateBack();
  }, 2000);
}

var isSubmit = function (that) {
  //var reg = '/^1[3|4|5|7|8][0-9]{9}$/'
  if (that.data.name && that.data.phone && that.data.address && that.data.weekL && that.data.weekR && that.data.timeL && that.data.timeR && that.data.businessAreaId) {
    that.setData({
      isSubmit: true
    })
  } else {
    that.setData({
      isSubmit: false
    })
  }
}